package lunceneChina;

import java.io.IOException;
import java.nio.file.FileSystems;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.QueryBuilder;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKTokenizer;

public class SerachFile {
	public static void main(String[] args) throws IOException {
		Directory directory=FSDirectory.open(FileSystems.getDefault().getPath("D:\\TestLunceneChina\\index_5.5"));
		Analyzer analyzer=new IKAnalyzer(true);
		IndexReader indexReader=DirectoryReader.open(directory);
		IndexSearcher searcher=new IndexSearcher(indexReader);
		
		

		QueryBuilder builder=new QueryBuilder(analyzer);
		Query query = builder.createPhraseQuery("content", "红楼梦");
		
		Query fuzzyQuery = new FuzzyQuery(new Term("content", "版本"));
		
		TopDocs search = searcher.search(fuzzyQuery, 10);
		System.out.println(search.totalHits);
		ScoreDoc[] scoreDocs = search.scoreDocs;
		for (ScoreDoc scoreDoc : scoreDocs) {
			Document document = searcher.doc(scoreDoc.doc);
			System.out.println(scoreDoc+"   "+document.get("filename"));
		}
		
	}
}
